import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'
axios.defaults.baseURL = "http://42.192.45.123:8088"

//前置拦截
axios.interceptors.request.use(config => {
    return config;
}, error => {
    return Promise.reject(error);
});
//后置拦截
//res返回的是数据而response返回的
axios.interceptors.response.use(response => {
    let res = response.data;
    if (res.code === 200) {
        console.log(response.headers.authorization);
        return response;
    } else {
        Element.Message.error(response.data.msg);
        return Promise.reject(response.data.msg)
    }
}, error => {
    //直接报错的情况
    console.log(error);
    //不为空的时候赋值错误消息
    if (error.response.data) {
        error.message = error.response.data.msg
    }
    if (error.response.data) {
        //store.commit(),跳转页面
        console.log(error.response.data)
        store.commit('REMOVE_INFO')
        router.push("/login")
    }
    return Promise.reject(error);
});